<?php

namespace app\service\cloud;

use RongCloud\RongCloud;
use support\Response;

/**
 * 融云IM composer require rongcloud/php-sdk
 * https://docs.rongcloud.cn/im/server/user/#token
 */
class RongYunService
{
    protected RongCloud $rong_cloud;
    protected mixed $appKey;
    protected mixed $appSecret;

    public function __construct()
    {
        $this->appKey = config("cloud.SonarCloud.appKey");
        $this->appSecret = config("cloud.SonarCloud.appSecret");
        $this->rong_cloud = new RongCloud(config("cloud.SonarCloud.appKey"), config("cloud.SonarCloud.appSecret"));
    }

    /**
     * API 调用签名规则
     * @return string
     */
    public function Signature(): string
    {
        // 重置随机数种子。
        srand((double)microtime() * 1000000);
        // 开发者平台分配的 App Secret。
        $appSecret = $this->appSecret;
        // 获取随机数。无长度限制
        $nonce = rand();
        // 获取时间戳（毫秒）。
        $timestamp = time() * 1000;
        //数据签名,生成本地签名。
        return sha1($appSecret . $nonce . $timestamp);
    }

//======================================================= 用户管理 ======================================================

    /**
     * 注册用户,获取token,用于登录融云
     * @param string $userId 用户 Id，支持大小写英文字母、数字、部分特殊符号 + = - _ 的组合方式，最大长度 64 字节。是用户在 App 中的唯一标识，必须保证在同一个 App 内不重复，重复的用户 Id 将被当作是同一用户。（必传）
     * @param string $name 用户名称，最大长度 128 字节。用来在 Push 推送时显示用户的名称，重新获取 Token 后将覆盖之前的用户名称。（必传）
     * @param string $portraitUri 用户头像 URI，最大长度 1024 字节。（必传）
     * @return mixed|string
     */
    public function getUserToken(string $userId, string $name, string $portraitUri): mixed
    {
        $data = [
            'id' => $userId,
            'name' => $name,
            'portrait' => $portraitUri,
        ];
        $data_result = $this->rong_cloud->getUser()->register($data);
        $code = $data_result['code'];
        if ($code == 200) {
            $token = $data_result['token'];
        } else {
            return errorMessage($data_result['msg']);
        }
        return $token;
    }

    /**
     * 作废 Token
     * @param array $user_data 用户id集合
     * @return bool|Response
     */
    public function expireUserToken(array $user_data): bool|Response
    {
        $timestamp = time() * 1000;
        $data = [
            'id' => $user_data,
            'time' => $timestamp
        ];
        $data_result = $this->rong_cloud->getUser()->expire($data);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 获取用户信息
     * @param string $userId 用户 ID
     * @return array|Response userName 用户名称  userPortrait 用户头像地址 createTime用户创建时间
     */
    public function getUserInfo(string $userId): array|Response
    {
        $data = [
            'id' => $userId
        ];
        $data_result = $this->rong_cloud->getUser()->get($data);
        $code = $data_result['code'];
        $data_user = [
            'name' => $data_result['userName'],
            'portrait' => $data_result['userPortrait'],
            'create_time' => $data_result['createTime'],
        ];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_user;
    }

    /**
     * 修改用户信息
     * @param string $userId 用户 ID，支持大小写英文字母与数字的组合，最大长度 64 字节。 userId 是用户在 App 中的唯一标识，必须保证在同一个 App 内不重复，重复的用户 ID 将被当作是同一用户。
     * @param string $name 用户名称，最大长度 64 个字符（不区分符号、英文字符、中文字符，统一限制最多 64 个字符）。用来在 Push 推送时，显示用户的名称，不提供则不进行刷新。
     * @param string $portraitUri 用户头像 URI，最大长度 1024 字节。注意：因为融云服务端不提供用户信息托管服务，所以不支持客户端 SDK 主动获取该用户头像数据。
     * @return bool|Response
     */
    public function getUserUpdate(string $userId, string $name, string $portraitUri): Response|bool
    {
        $data = [
            'id' => $userId,
            'name' => $name,
            'portrait' => $portraitUri,
        ];
        $data_result = $this->rong_cloud->getUser()->update($data);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 查询用户在线状态
     * @param string $userId 用户 ID
     * @return array|int[] status 在线状态，1为在线，0为不在线
     */
    public function getUserOnlineStatus(string $userId): array
    {
        $user = [
            'id' => $userId,
        ];
        $data_result = $this->rong_cloud->getUser()->Onlinestatus()->check($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return ["status" => 0];
        }
        return ['status' => $data_result['status']];
    }

    /**
     * 注销用户
     * @param string $userId
     * @return Response|bool
     */
    public function getUserAbandon(string $userId): Response|bool
    {
        $user = [
            'id' => $userId, //用户id
        ];
        $data_result = $this->rong_cloud->getUser()->abandon($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 查询已注销用户
     * @param int $page 页码
     * @param int $size 分页条数
     * @return mixed|Response
     */
    public function getUserAbandonQuery(int $page = 1, int $size = 10): mixed
    {
        $params = [
            'page' => $page,
            'size' => $size,
        ];
        $data_result = $this->rong_cloud->getUser()->abandonQuery($params);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_result['users'];
    }

    /**
     * 注销用户重新激活
     * @param string $userId 用户id
     * @return Response|bool
     */
    public function getUserActivate(string $userId): Response|bool
    {
        $user = [
            'id' => $userId,
        ];
        $data_result = $this->rong_cloud->getUser()->activate($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 封禁用户
     * @param string $userId
     * @return Response|bool
     */
    public function addUserBlock(string $userId): Response|bool
    {
        $user = [
            'id' => $userId, //用户id
            'minute' => 20,//封禁时长 1 - 1 * 30 * 24 * 60 分钟
        ];
        $data_result = $this->rong_cloud->getUser()->Block()->add($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 解除用户封禁
     * @param string $userId
     * @return Response|bool
     */
    public function getUserBlockRemove(string $userId): Response|bool
    {
        $user = [
            'id' => $userId, //解禁用户id 唯一标识，最大长度 30 个字符
        ];
        $data_result = $this->rong_cloud->getUser()->Block()->remove($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 获取封禁用户列表
     * @param int $page
     * @param int $size
     * @return mixed|Response
     */
    public function getUserBlockList(int $page = 1, int $size = 10): mixed
    {
        $user = [
            'page' => $page,
            'size' => $size,
        ];
        $data_result = $this->rong_cloud->getUser()->Block()->getList($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_result['users'];
    }

    /**
     * 设置用户单聊禁言
     * @param array $data_user 被禁言用户 Id，支持批量设置，最多不超过 1000 个
     * @param int $state 禁言状态，0 解除禁言、1 添加禁言
     * @param string $type 会话类型，目前支持单聊会话 PERSON。
     * @return Response|true 会话类型，目前支持单聊会话 PERSON
     */
    public function addUserBan(array $data_user, int $state = 1, string $type = "PERSON"): Response|bool
    {
        $user = [
            'id' => $data_user,
            'state' => $state,
            'type' => $type,
        ];
        $data_result = $this->rong_cloud->getUser()->Ban()->set($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 查询单聊禁言用户列表
     * @param int $num 获取行数，默认为 100，最大支持 200 个。
     * @param int $offset 查询开始位置，默认为 0
     * @param string $type 会话类型，目前支持单聊会话 PERSON。
     * @return mixed|Response
     */
    public function getUserBanList(int $num = 100, int $offset = 0, string $type = "PERSON"): mixed
    {
        $user = [
            'num' => $num,
            'offset' => $offset,
            'type' => $type
        ];
        $data_result = $this->rong_cloud->getUser()->Ban()->getList($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_result['users'];
    }

    /**
     * 设置用户标签
     * @param string $userId 用户 ID
     * @param array $tags 用户标签，一个用户最多添加 20 个标签，每个 tag 最大不能超过 40 个字节，标签中不能包含特殊字符。每次设置时需要传入用户的全量标签数据。传入空数组表示清除该用户的所有标签。
     * @return Response|true
     */
    public function addUserTag(string $userId, array $tags): Response|bool
    {
        $user = [
            'userId' => $userId,
            'tags' => $tags,
        ];
        $data_result = $this->rong_cloud->getUser()->Tag()->set($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 批量设置用户标签
     * @param array $userIds 用户 ID，一次最多支持 1000 个用户。传入的所有用户的标签都被会覆盖更新为 tags 中的标签。
     * @param array $tags 用户标签，一个用户最多添加 20 个标签，每个 tag 最大不能超过 40 个字节，标签中不能包含特殊字符。每次设置时需要传入全量标签数据。传入空数组表示清除用户的所有标签。
     * @return Response|true
     */
    public function addBatchUserTag(array $userIds, array $tags): Response|bool
    {
        $user = [
            'userIds' => $userIds,
            'tags' => $tags,
        ];
        $data_result = $this->rong_cloud->getUser()->Tag()->batchset($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 获取用户标签
     * @param array $userIds
     * @return Response|array
     */
    public function getUserTagList(array $userIds): Response|array
    {
        $user = [
            'userIds' => $userIds,
        ];
        $data_result = $this->rong_cloud->getUser()->Tag()->get($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_result;
    }

//============================================ 用户黑/白名单服务 ==========================================================

    /**
     * 添加用户到黑名单
     * @param string $userId 用户 ID
     * @param array $blacklist 被加入黑名单的用户 ID。单次可添加最多 20 个 blackUserId。
     * @return Response|bool|array
     */
    public function addUserBlack(string $userId, array $blacklist): Response|bool|array
    {
        $user = [
            'userId' => $userId,
            'blacklist' => $blacklist,
        ];
        $data_result = $this->rong_cloud->getUser()->Blacklist()->add($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 移除黑名单中用户
     * @param string $userId 用户 ID
     * @param array $blacklist 被加入黑名单的用户 ID。单次可添加最多 20 个 blackUserId。
     * @return Response|bool|array
     */
    public function removeUserBlack(string $userId, array $blacklist): Response|bool|array
    {
        $user = [
            'userId' => $userId,
            'blacklist' => $blacklist,
        ];
        $data_result = $this->rong_cloud->getUser()->Blacklist()->remove($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }


    /**
     * 获取黑名单用户列表
     * @param string $userId 用户 ID
     * @return Response|bool|array
     */
    public function getUserBlackList(string $userId): Response|bool|array
    {
        $user = [
            'userId' => $userId,
        ];
        $data_result = $this->rong_cloud->getUser()->Blacklist()->getList($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_result['users'];
    }


    /**
     * 添加用户到白名单
     * @param string $userId 用户 ID
     * @param array $blacklist 被加入白名单的用户 ID。单次可添加最多 20 个 whiteUserId
     * @return Response|bool|array
     */
    public function addUserBlackWhitelist(string $userId, array $blacklist): Response|bool|array
    {
        $user = [
            'userId' => $userId,
            'whitelist' => $blacklist,
        ];
        $data_result = $this->rong_cloud->getUser()->Blacklist()->add($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 添加用户到白名单
     * @param string $userId 用户 ID
     * @param array $blacklist 被加入白名单的用户 ID。单次可添加最多 20 个 whiteUserId
     * @return Response|bool|array
     */
    public function removeUserBlackWhitelist(string $userId, array $blacklist): Response|bool|array
    {
        $user = [
            'userId' => $userId,
            'whitelist' => $blacklist,
        ];
        $data_result = $this->rong_cloud->getUser()->Blacklist()->remove($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }


    /**
     * 查询白名单中用户列表
     * @param string $userId 用户 ID
     * @return Response|bool|array
     */
    public function getUserWhitelist(string $userId): Response|bool|array
    {
        $user = [
            'userId' => $userId,
        ];
        $data_result = $this->rong_cloud->getUser()->Whitelist()->getList($user);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_result['users'];
    }

//==================================== 消息管理 =========================================================================

    /**
     * 发送单聊普通消息
     * @param string $senderId 发送人用户 ID。
     * @param array $targetId 接收用户 ID
     * @param string $objectName 消息类型
     * @param string $content 所发送消息的内容
     * @return Response|true
     */
    public function sendMessagePerson(string $senderId, array $targetId, string $content, string $objectName = 'RC:TxtMsg'): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            'targetId' => $targetId,
            "objectName" => $objectName,
            'content' => json_encode(['content' => $content])
        ];
        $data_result = $this->rong_cloud->getMessage()->Person()->send($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 发送单聊状态消息
     * @param string $senderId 发送人用户 ID。
     * @param array $targetId 接收用户 ID
     * @param string $objectName 消息类型
     * @param string $content 所发送消息的内容
     * @return Response|true
     */
    public function sendMessageStatusPerson(string $senderId, array $targetId, string $content, string $objectName = 'RC:TxtMsg'): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            'targetId' => $targetId,
            "objectName" => $objectName,
            'content' => json_encode(['content' => $content])
        ];
        $data_result = $this->rong_cloud->getMessage()->Person()->sendStatusMessage($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 发送单聊模板消息
     * @param string $senderId 发送人用户 ID
     * @param string $objectName 消息类型
     * @param string $template_content 模板内容
     * @param array $content 所发送消息的内容  ['接收人 id'=>['data'=>'模板数据','push'=>'推送内容']
     * @return Response|true
     */
    public function sendTemplateMessagePerson(string $senderId, string $template_content, array $content, string $objectName = 'RC:TxtMsg'): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            "objectName" => $objectName,
            'template' => json_encode(['content' => $template_content]),
            'content' => json_encode($content),
        ];
        $data_result = $this->rong_cloud->getMessage()->Person()->sendTemplate($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }


    /**
     * 发送群聊消息
     * @param string $senderId 发送人用户 ID。
     * @param array $targetId 群组 id
     * @param string $objectName 消息类型
     * @param string $content 所发送消息的内容
     * @return Response|true
     */
    public function sendMessageGroup(string $senderId, array $targetId, string $content, string $objectName = 'RC:TxtMsg'): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            'targetId' => $targetId,
            "objectName" => $objectName,
            'content' => json_encode(['content' => $content])
        ];
        $data_result = $this->rong_cloud->getMessage()->Group()->send($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 发送群聊状态消息
     * @param string $senderId 发送人用户 ID。
     * @param array $targetId 群组 id
     * @param string $objectName 消息类型
     * @param string $content 所发送消息的内容
     * @return Response|true
     */
    public function sendMessageStatusGroup(string $senderId, array $targetId, string $content, string $objectName = 'RC:TxtMsg'): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            'targetId' => $targetId,
            "objectName" => $objectName,
            'content' => json_encode(['content' => $content])
        ];
        $data_result = $this->rong_cloud->getMessage()->Group()->sendStatusMessage($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 发送群聊 @ 消息
     * @param string $senderId 发送人用户 ID。
     * @param array $targetId 群组 id
     * @param string $content 消息内容
     * @param array $userIds 被 @ 人列表 type 为 2 时必填，type 为 1 时可以为空
     * @param string $pushContent
     * @param int $type 功能类型，1 表示 @ 所有人、2 表示 @ 指定用户
     * @param string $objectName 消息类型
     * @return Response|bool
     */
    public function sendMentionMessageGroup(string $senderId, array $targetId, string $content, string $pushContent, array $userIds = [], int $type = 1, string $objectName = 'RC:TxtMsg'): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            'targetId' => $targetId,
            "objectName" => $objectName,
            'content' => json_encode([
                'content' => $content,
                'mentionedInfo' => [
                    'type' => $type,//@ 功能类型，1 表示 @ 所有人、2 表示 @ 指定用户
                    'userIds' => $userIds,//被 @ 人列表 type 为 2 时必填，type 为 1 时可以为空
                    'pushContent' => $pushContent//自定义 @ 消息 push 内容
                ]
            ])
        ];
        $data_result = $this->rong_cloud->getMessage()->Group()->sendMention($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 撤回已发送的群聊消息
     * @param string $senderId 发送人 Id
     * @param array $targetId 群组 id
     * @param string $uId 消息的唯一标识
     * @param string $sentTime 消息的发送时间
     * @return Response|bool
     */
    public function recallMessageGroup(string $senderId, array $targetId, string $uId, string $sentTime): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            'targetId' => $targetId,
            "uId" => $uId,
            'sentTime' => $sentTime
        ];
        $data_result = $this->rong_cloud->getMessage()->Group()->recall($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }


    /**
     * 超级群消息发送
     * @param string $senderId 发送人ID
     * @param array $targetId 超级群 id
     * @param string $objectName 消息类型
     * @param string $content 所发送消息的内容
     * @return Response|true
     */
    public function sendMessageUltraGroup(string $senderId, array $targetId, string $content, string $objectName = 'RC:TxtMsg'): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            'targetId' => $targetId,
            "objectName" => $objectName,
            'content' => json_encode(['content' => $content])
        ];
        $data_result = $this->rong_cloud->getMessage()->Ultragroup()->send($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 超级群消息发送 @ 消息
     * @param string $senderId 发送人用户 ID。
     * @param array $targetId 超级群 id
     * @param string $content 消息内容
     * @param array $userIds 被 @ 人列表 type 为 2 时必填，type 为 1 时可以为空
     * @param string $pushContent
     * @param int $type 功能类型，1 表示 @ 所有人、2 表示 @ 指定用户
     * @param string $objectName 消息类型
     * @return Response|bool
     */
    public function sendMentionMessageUltraGroup(string $senderId, array $targetId, string $content, string $pushContent, array $userIds = [], int $type = 1, string $objectName = 'RC:TxtMsg'): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            'targetId' => $targetId,
            "objectName" => $objectName,
            'content' => json_encode([
                'content' => $content,
                'mentionedInfo' => [
                    'type' => $type,//@ 功能类型，1 表示 @ 所有人、2 表示 @ 指定用户
                    'userIds' => $userIds,//被 @ 人列表 type 为 2 时必填，type 为 1 时可以为空
                    'pushContent' => $pushContent//自定义 @ 消息 push 内容
                ]
            ])
        ];
        $data_result = $this->rong_cloud->getMessage()->Ultragroup()->sendMention($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 超级群消息撤回
     * @param string $senderId 发送人 Id
     * @param array $targetId 群组 id
     * @param string $uId 消息的唯一标识
     * @param string $sentTime 消息的发送时间
     * @return Response|bool
     */
    public function recallMessageUltraGroup(string $senderId, array $targetId, string $uId, string $sentTime): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            'targetId' => $targetId,
            "uId" => $uId,
            'sentTime' => $sentTime
        ];
        $data_result = $this->rong_cloud->getMessage()->Ultragroup()->recall($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }


    /**
     * 聊天室发送消息
     * @param string $senderId 发送人ID
     * @param array $targetId 聊天室 id
     * @param string $objectName 消息类型
     * @param string $content 消息内容
     * @return Response|true
     */
    public function sendMessageChatroom(string $senderId, array $targetId, string $content, string $objectName = 'RC:TxtMsg'): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            'targetId' => $targetId,
            "objectName" => $objectName,
            'content' => json_encode(['content' => $content])
        ];
        $data_result = $this->rong_cloud->getMessage()->Chatroom()->send($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 撤回已发送的聊天室消息
     * @param string $senderId 发送人ID
     * @param array $targetId 聊天室 id
     * @param string $uId 消息的唯一标识
     * @param string $sentTime 消息的发送时间
     * @return Response|true
     */
    public function recallMessageChatroom(string $senderId, array $targetId, string $uId, string $sentTime): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            'targetId' => $targetId,
            "uId" => $uId,
            'sentTime' => $sentTime
        ];
        $data_result = $this->rong_cloud->getMessage()->Chatroom()->send($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 聊天室广播消息
     * @param string $senderId 发送人ID
     * @param string $objectName 消息类型
     * @param string $content 消息内容
     * @return Response|true
     */
    public function sendBroadcastMessageChatroom(string $senderId, string $content, string $objectName = 'RC:TxtMsg'): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            "objectName" => $objectName,
            'content' => json_encode(['content' => $content])
        ];
        $data_result = $this->rong_cloud->getMessage()->Chatroom()->broadcast($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 广播消息撤回
     * @param string $senderId 发送人 id
     * @param string $uId 消息唯一标识，通过 /push 发送广播消息后获取，返回名称为 id。
     * @param int $isAdmin 是否为管理员，默认为 0；设为 1 时 IMKit SDK 收到此条消息后，小灰条默认显示为“管理员 撤加了一条消息”
     * @param int $isDelete 是否删除消息，默认为 0 撤回该条消息同时，用户端将该条消息删除并替换为一条小灰条撤回提示消息；为 1 时，该条消息删除后，不替换为小灰条提示消息。
     * @param string $objectName 消息类型
     * @return Response|bool
     */
    public function recallBroadcastMessageChatroom(string $senderId, string $uId, int $isAdmin = 0, int $isDelete = 0, string $objectName = "RC:RcCmd"): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            'objectName' => $objectName,
            'content' => json_encode([
                'uId' => $uId,
                'isAdmin' => '0',
                'isDelete' => '0'
            ])
        ];
        $data_result = $this->rong_cloud->getMessage()->Broadcast()->recall($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 设置单群聊消息扩展
     * @param string $msgUID 消息唯一标识 ID，服务端可通过全量消息路由功能获取
     * @param string $userId 需要设置扩展的消息发送用户 Id。
     * @param string $targetId 目标 Id，根据不同的 conversationType，可能是用户 Id 或群组 Id。
     * @param array $extraKeyVal 消息自定义扩展内容，JSON 结构，以 Key、Value 的方式进行设置
     * @param string $conversationType 会话类型，二人会话是 1 、群组会话是 3，只支持单聊、群组会话类型。
     * @param int $isSyncSender 终端用户在线状态下，发送者是否接收该设置状态，0 表示为不接收，1 表示为接收，默认为 0 不接收
     * @return Response|bool
     */
    public function setMessageExpansion(string $msgUID, string $userId, string $targetId, array $extraKeyVal, string $conversationType = '1', int $isSyncSender = 0): Response|bool
    {
        $message = [
            'msgUID' => $msgUID,
            'userId' => $userId,
            'targetId' => $targetId,
            'conversationType' => $conversationType,
            'extraKeyVal' => $extraKeyVal,
            'isSyncSender' => $isSyncSender
        ];
        $data_result = $this->rong_cloud->getMessage()->Expansion()->set($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 设置单群聊消息扩展
     * @param string $msgUID 消息唯一标识 ID，服务端可通过全量消息路由功能获取
     * @param string $userId 需要设置扩展的消息发送用户 Id。
     * @param string $targetId 目标 Id，根据不同的 conversationType，可能是用户 Id 或群组 Id。
     * @param array $extraKeyVal 需要删除的扩展信息的 Key 值，一次最多可以删除 100 个扩展信息
     * @param string $conversationType 会话类型，二人会话是 1 、群组会话是 3，只支持单聊、群组会话类型。
     * @param int $isSyncSender 终端用户在线状态下，发送者是否接收该设置状态，0 表示为不接收，1 表示为接收，默认为 0 不接收
     * @return Response|bool
     */
    public function deleteMessageExpansion(string $msgUID, string $userId, string $targetId, array $extraKeyVal, string $conversationType = '1', int $isSyncSender = 0): Response|bool
    {
        $message = [
            'msgUID' => $msgUID,
            'userId' => $userId,
            'targetId' => $targetId,
            'conversationType' => $conversationType,
            'extraKey' => $extraKeyVal,
            'isSyncSender' => $isSyncSender
        ];
        $data_result = $this->rong_cloud->getMessage()->Expansion()->delete($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 获取单群聊消息扩展
     * @param string $msgUID 消息唯一标识 ID，服务端可通过全量消息路由功能获取。
     * @param int $pageNo 页数，默认返回 300 个扩展信息。
     * @return Response|bool
     */
    public function getMessageExpansionList(string $msgUID, int $pageNo = 1): Response|bool
    {
        $message = [
            'msgUID' => $msgUID,
            'pageNo' => $pageNo
        ];
        $data_result = $this->rong_cloud->getMessage()->Expansion()->getList($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_result['extraContent'];
    }


    /**
     * 获取历史消息日志
     * @param string $date 日期
     * @return Response|bool
     */
    public function getMessageHistoryList(string $date = ""): Response|bool
    {
        if (empty($date)) {
            $date = date('YmdH');
        }
        $message = [
            'date' => $date,
        ];
        $data_result = $this->rong_cloud->getMessage()->History()->get($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_result['url'];
    }

    /**
     * 删除历史消息日志
     * @param string $date 日期
     * @return Response|bool
     */
    public function removeMessageHistory(string $date = ""): Response|bool
    {
        if (empty($date)) {
            $date = date('YmdH');
        }
        $message = [
            'date' => $date,
        ];
        $data_result = $this->rong_cloud->getMessage()->History()->remove($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 清除历史消息
     * @param string $fromUserId 用户 ID，删除该用户指定会话 msgTimestamp 前的历史消息
     * @param string $targetId 需要清除的目标会话 ID
     * @param string $msgTimestamp 清除该时间戳之前的所有历史消息，精确到毫秒，为空时清除该会话的所有历史消息。
     * @param string $conversationType 会话类型，支持单聊、群聊、系统会话。单聊会话是 1、群组会话是 3、系统通知是 6
     * @return Response|bool
     */
    public function cleanMessageHistory(string $fromUserId, string $targetId, string $msgTimestamp = "", string $conversationType = "1"): Response|bool
    {
        $message = [
            'conversationType' => $conversationType,
            'fromUserId' => $fromUserId,
            'targetId' => $targetId,
            'msgTimestamp' => $msgTimestamp,
        ];
        $data_result = $this->rong_cloud->getMessage()->History()->clean($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 系统消息发送
     * @param string $senderId 发送人 id
     * @param string $targetId 接收放 id
     * @param string $content 消息体
     * @param string $objectName 消息类型 文本
     * @return Response|true
     */
    public function sendMessageSystem(string $senderId, string $targetId, string $content, string $objectName = 'RC:TxtMsg'): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            'targetId' => $targetId,
            "objectName" => $objectName,
            'content' => ['content' => $content]
        ];
        $data_result = $this->rong_cloud->getMessage()->System()->send($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 系统广播消息
     * @param string $senderId 发送人 id
     * @param string $content 消息体
     * @param string $objectName 消息类型 文本
     * @return Response|true
     */
    public function broadcastMessageSystem(string $senderId, string $content, string $objectName = 'RC:TxtMsg'): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            "objectName" => $objectName,
            'content' => ['content' => $content]
        ];
        $data_result = $this->rong_cloud->getMessage()->System()->broadcast($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 在线用户广播
     * @param string $senderId 发送人 id
     * @param string $content 消息体
     * @param string $objectName 消息类型 文本
     * @return Response|true
     */
    public function onlineBroadcastMessageSystem(string $senderId, string $content, string $objectName = 'RC:TxtMsg'): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            "objectName" => $objectName,
            'content' => ['content' => $content]
        ];
        $data_result = $this->rong_cloud->getMessage()->System()->onlineBroadcast($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 系统模板消息
     * @param string $senderId 发送人用户 ID
     * @param string $objectName 消息类型
     * @param string $template_content 模板内容
     * @param array $content 所发送消息的内容  ['接收人 id'=>['data'=>'模板数据','push'=>'推送内容']
     * @return Response|true
     */
    public function sendTemplateMessageSystem(string $senderId, string $template_content, array $content, string $objectName = 'RC:TxtMsg'): Response|bool
    {
        $message = [
            'senderId' => $senderId,
            "objectName" => $objectName,
            'template' => json_encode(['content' => $template_content]),
            'content' => json_encode($content),
        ];
        $data_result = $this->rong_cloud->getMessage()->System()->sendTemplate($message);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

//======================================== 群组管理 =====================================================================

    /**
     * 创建群组
     * @param string $group_id 群组id
     * @param string $name 群组名称
     * @param array $members 群成员列表 ['id'=> 'uPj70HUrRSUk-ixtt7iIGc'],['id'=>'Vu-oC0_LQ6kgPqltm_zYtI']
     * @return Response|true
     */
    public function crateGroup(string $group_id, string $name, array $members): Response|bool
    {
        $group = [
            'id' => $group_id,
            'name' => $name,
            'members' => $members
        ];
        $data_result = $this->rong_cloud->getGroup()->create($group);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 修改群信息
     * @param string $group_id 群组id
     * @param string $name 群组名称
     * @return Response|true
     */
    public function updateGroup(string $group_id, string $name): Response|bool
    {
        $group = [
            'id' => $group_id,
            'name' => $name
        ];
        $data_result = $this->rong_cloud->getGroup()->update($group);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 获取群信息
     * @param string $group_id 群组id
     * @return array|bool|Response
     */
    public function getGroup(string $group_id): Response|bool|array
    {
        $group = [
            'id' => $group_id,
        ];
        $data_result = $this->rong_cloud->getGroup()->get($group);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_result['groups'];
    }

    /**
     * 加入群组
     * @param string $group_id 群组id
     * @param string $group_name 群组名称
     * @param string $userId 群成员信息
     * @return Response|true
     */
    public function joinsGroup(string $group_id, string $group_name, string $userId): Response|bool
    {
        $group = [
            'id' => $group_id,
            'name' => $group_name,
            'member' => ['id' => $userId],//群成员信息
        ];
        $data_result = $this->rong_cloud->getGroup()->joins($group);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 退出群组
     * @param string $group_id 群组id
     * @param string $userId 退出人员信息
     * @return Response|true
     */
    public function quitGroup(string $group_id, string $userId): Response|bool
    {
        $group = [
            'id' => $group_id,
            'member' => ['id' => $userId],//群成员信息
        ];
        $data_result = $this->rong_cloud->getGroup()->quit($group);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 解散群组
     * @param string $group_id 群组id
     * @param string $userId 管理员信息
     * @return Response|true
     */
    public function dismissGroup(string $group_id, string $userId): Response|bool
    {
        $group = [
            'id' => $group_id,
            'member' => ['id' => $userId],
        ];
        $data_result = $this->rong_cloud->getGroup()->dismiss($group);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 群组信息同步
     * @param string $userId 用户id
     * @param array $groups 用户群组信息 ['id'=> '群Id', 'name'=> '群名称']
     * @return Response|bool
     */
    public function syncGroup(string $userId, array $groups): Response|bool
    {
        $group = [
            'id' => $userId,
            'groups' => $groups,
        ];
        $data_result = $this->rong_cloud->getGroup()->dismiss($group);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 添加群聊备注
     * @param string $groupId 群组id
     * @param string $userId 人员id
     * @param string $remark 群组备注
     * @return Response|true
     */
    public function addRemarkGroup(string $groupId, string $userId, string $remark): Response|bool
    {
        $group = [
            'userId' => $userId,
            'groupId' => $groupId,
            'remark' => $remark
        ];
        $data_result = $this->rong_cloud->getGroup()->Remark()->set($group);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 删除群聊备注
     * @param string $groupId 群组id
     * @param string $userId 人员id
     * @return Response|true
     */
    public function delRemarkGroup(string $groupId, string $userId): Response|bool
    {
        $group = [
            'userId' => $userId,
            'groupId' => $groupId
        ];
        $data_result = $this->rong_cloud->getGroup()->Remark()->del($group);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 获取群聊备注
     * @param string $groupId 群组id
     * @param string $userId 人员id
     * @return Response|true
     */
    public function getRemarkGroup(string $groupId, string $userId): Response|bool
    {
        $group = [
            'userId' => $userId,
            'groupId' => $groupId
        ];
        $data_result = $this->rong_cloud->getGroup()->Remark()->get($group);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }



//============================================ 群组禁言服务 ==============================================================

    /**
     * 添加群组禁言
     * @param string $group_id 群组 id
     * @param string $userId 禁言人员列表
     * @param int $minute 禁言时长
     * @return Response|true
     */
    public function addGagGroup(string $group_id, string $userId, int $minute = 3000): Response|bool
    {
        $group = [
            'id' => $group_id,
            'members' => [
                ['id' => $userId]
            ],
            'minute' => $minute
        ];
        $result = $this->rong_cloud->getGroup()->Gag()->add($group);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 取消指定群成员禁言
     * @param string $group_id 群组 id
     * @param string $userId 禁言人员列表
     * @param int $minute 禁言时长
     * @return Response|true
     */
    public function removeGagGroup(string $group_id, string $userId, int $minute = 3000): Response|bool
    {
        $group = [
            'id' => $group_id,
            'members' => [
                ['id' => $userId]
            ],
        ];
        $result = $this->rong_cloud->getGroup()->Gag()->remove($group);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 查询群成员禁言列表
     * @param string $group_id 群组 id
     * @return mixed|Response
     */
    public function getGagGroupList(string $group_id): mixed
    {
        $group = [
            'id' => $group_id,
        ];
        $result = $this->rong_cloud->getGroup()->Gag()->getList($group);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return $result['users'];
    }


    /**
     * 设置群组全体禁言
     * @param string $group_id 群组 id
     * @return Response|true
     */
    public function addMuteAllMembersGroup(string $group_id): Response|bool
    {
        $group = [
            'id' => $group_id,
        ];
        $result = $this->rong_cloud->getGroup()->MuteAllMembers()->add($group);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }


    /**
     * 取消群组全体禁言
     * @param string $group_id 群组 id
     * @return Response|true
     */
    public function removeMuteAllMembersGroup(string $group_id): Response|bool
    {
        $group = [
            'id' => $group_id,
        ];
        $result = $this->rong_cloud->getGroup()->MuteAllMembers()->remove($group);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 查询群组全体禁言白名单
     * @param string $group_id 群组 id
     * @return mixed|Response
     */
    public function getMuteAllGroupList(string $group_id = ""): mixed
    {
        if (empty($group_id)) {
            $group = [];
        } else {
            $group = [
                'id' => $group_id,
            ];
        }
        $result = $this->rong_cloud->getGroup()->MuteAllMembers()->getList($group);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return $result['groupinfo'];
    }


    /**
     * 加入群组全体禁言白名单
     * @param string $group_id 群组 id
     * @param string $userId 禁言白名单人员列表
     * @return Response|true
     */
    public function addMuteWhiteListMembersGroup(string $group_id, string $userId): Response|bool
    {
        $group = [
            'id' => $group_id,
            'members' => [
                ['id' => $userId]
            ]
        ];
        $result = $this->rong_cloud->getGroup()->MuteWhiteList()->add($group);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 移出群组全体禁言白名单
     * @param string $group_id 群组 id
     * @return Response|true
     */
    public function removeMuteWhiteListMembersGroup(string $group_id, string $userId): Response|bool
    {
        $group = [
            'id' => $group_id,
            'members' => [
                ['id' => $userId]
            ]
        ];
        $result = $this->rong_cloud->getGroup()->MuteWhiteList()->remove($group);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 查询群组全体禁言白名单
     * @param string $group_id 群组 id
     * @return mixed|Response
     */
    public function getMuteWhiteListGroupList(string $group_id): mixed
    {
        $group = [
            'id' => $group_id,
        ];
        $result = $this->rong_cloud->getGroup()->MuteWhiteList()->getList($group);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return $result['userIds'];
    }

//===================================== 聊天室管理 ======================================================================

    /**
     * 创建聊天室
     * @param string $chatroomId 聊天室id
     * @param array $whiteUserIds 禁言白名单用户列表，支持批量设置，最多不超过 20 个
     * @param int $destroyType 指定聊天室的销毁类型 0：默认值，表示不活跃时销毁,1：固定时间销毁
     * @param bool $isBan 是否禁言聊天室全体成员，默认 false
     * @return Response|true
     */
    public function createChatroom(string $chatroomId, array $whiteUserIds, int $destroyType = 0, bool $isBan = true): Response|bool
    {
        $chatroom = [
            'id' => $chatroomId,
            'destroyType' => $destroyType,
            'isBan' => $isBan,
            'whiteUserIds' => $whiteUserIds
        ];
        $result = $this->rong_cloud->getChatroom()->createV2($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 查询聊天室
     * @param string $chatroomId 聊天室id
     * @return Response|array
     */
    public function queryChatroom(string $chatroomId): Response|array
    {
        $chatroom = [
            'id' => $chatroomId
        ];
        $result = $this->rong_cloud->getChatroom()->queryV2($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return $result;
    }

    /**
     * 获取聊天室成员
     * @param string $chatroomId 聊天室 id
     * @param int $count 要获取的聊天室成员信息数，最多返回 500 个成员信息
     * @param int $order 加入聊天室的先后顺序， 1 为加入时间正序， 2 为加入时间倒序
     * @return Response|array
     */
    public function getChatroom(string $chatroomId, int $count = 10, int $order = 1): Response|array
    {
        $chatroom = [
            'id' => $chatroomId,
            'count' => $count,
            'order' => $order
        ];
        $result = $this->rong_cloud->getChatroom()->get($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return $result;
    }

    /**
     * 设置聊天室销毁类型
     * @param string $chatroomId 聊天室 id
     * @param int $destroyType 指定聊天室的销毁方式。0：默认值，表示不活跃时销毁。默认情况下，所有聊天室的自动销毁方式均为不活跃时销毁，一旦不活跃长达到 60 分钟即被销毁，可通过 destroyTime 延长该时间。1：固定时间销毁，设置为该类型后，聊天室默认在创建 60 分钟后自动销毁，可通过 destroyTime 设置更长的存活时间
     * @param int $destroyTime 设置聊天室销毁时间。在 destroyType=0 时，表示聊天室应在不活跃达到该时长时自动销毁。在 destroyType=1 时，表示聊天室应在创建以后存活时间达到该时长后自动销毁。单位为分钟，最小值 60 分钟，最大 10080 分钟（7 天）。如果未设置，默认 60 分钟。
     * @return array|Response|true
     */
    public function setDestroyTypeChatroom(string $chatroomId, int $destroyType = 0, int $destroyTime = 60): Response|bool|array
    {
        $chatroom = [
            'id' => $chatroomId,
            'destroyType' => $destroyType,
            'destroyTime' => $destroyTime
        ];
        $result = $this->rong_cloud->getChatroom()->setDestroyType($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }


    /**
     * 销毁聊天室
     * @param string $chatroomId 聊天室id
     * @return Response|array
     */
    public function destoryChatroom(string $chatroomId): Response|array
    {
        $chatroom = [
            'id' => $chatroomId
        ];
        $result = $this->rong_cloud->getChatroom()->destory($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return $result;
    }

    /**
     * 查询是否在聊天室中
     * @param string $chatroomId 聊天室id
     * @param string $userId 人员id
     * @return Response|array
     */
    public function isExistUserChatroom(string $chatroomId, string $userId): Response|array
    {
        $chatroom = [
            'id' => $chatroomId,
            'members' => [
                ['id' => $userId]
            ]
        ];
        $result = $this->rong_cloud->getChatroom()->destory($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return $result;
    }

    /**
     * 全局禁言用户
     * @param string $userId 人员 id
     * @param int $minute 禁言时长
     * @return array|Response|true
     */
    public function addBanChatroom(string $userId, int $minute = 30): Response|bool|array
    {
        $chatroom = [
            'members' => [
                ['id' => $userId]
            ],
            'minute' => $minute
        ];
        $result = $this->rong_cloud->getChatroom()->Ban()->add($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 取消全局禁言用户
     * @param string $userId 人员 id
     * @return array|Response|true
     */
    public function removeBanChatroom(string $userId): Response|bool|array
    {
        $chatroom = [
            'members' => [
                ['id' => $userId]
            ]
        ];
        $result = $this->rong_cloud->getChatroom()->Ban()->remove($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 获取聊天室全局禁言列表
     * @return mixed|Response|null
     */
    public function getListBanChatroom(): mixed
    {
        $chatroom = [

        ];
        $result = $this->rong_cloud->getChatroom()->Ban()->getList($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return $result;
    }

    /**
     * 添加聊天室人员封禁
     * @param string $chatroomId 聊天室id
     * @param string $userId 人员 id
     * @param int $minute 禁言时长
     * @return array|Response|true
     */
    public function addBlockChatroom(string $chatroomId, string $userId, int $minute = 30): Response|bool|array
    {
        $chatroom = [
            'id' => $chatroomId,
            'members' => [
                ['id' => $userId]
            ],
            'minute' => $minute
        ];
        $result = $this->rong_cloud->getChatroom()->Block()->add($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }


    /**
     * 解除聊天室用户禁言
     * @param string $chatroomId 聊天室 id
     * @param string $userId 人员 id
     * @return array|Response|true
     */
    public function removeBlockChatroom(string $chatroomId, string $userId): Response|bool|array
    {
        $chatroom = [
            'id' => $chatroomId,
            'members' => [
                ['id' => $userId]
            ],
        ];
        $result = $this->rong_cloud->getChatroom()->Block()->remove($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 查询聊天室被封禁成员列表
     * @param string $chatroomId 聊天室 id
     * @return mixed
     */
    public function getListBlockChatroom(string $chatroomId): mixed
    {
        $chatroom = [
            'id' => $chatroomId
        ];
        $result = $this->rong_cloud->getChatroom()->Block()->getList($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return $result['users'];
    }

    /**
     * 添加应用内聊天室降级消息
     * @param array $msgs 消息类型列表 ['RC:TxtMsg03','RC:TxtMsg02']
     * @return array|Response|true
     */
    public function addDemotionChatroom(array $msgs): Response|bool|array
    {
        $chatroom = [
            'msgs' => $msgs
        ];
        $result = $this->rong_cloud->getChatroom()->Demotion()->add($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 移除应用内聊天室降级消息
     * @param array $msgs $msgs
     * @return array|Response|true
     */
    public function removeDemotionChatroom(array $msgs): Response|bool|array
    {
        $chatroom = [
            'msgs' => $msgs
        ];
        $result = $this->rong_cloud->getChatroom()->Demotion()->remove($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 获取应用内聊天室降级消息
     * @return mixed
     */
    public function getListDemotionChatroom(): mixed
    {
        $chatroom = [
        ];
        $result = $this->rong_cloud->getChatroom()->Demotion()->getList($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return $result['objectNames'];
    }

    /**
     * 停止聊天室消息分发
     * @param string $chatroomId 聊天室 id
     * @return bool|Response
     */
    public function stopDistributeChatroom(string $chatroomId): bool|Response
    {
        $chatroom = [
            'id' => $chatroomId
        ];
        $result = $this->rong_cloud->getChatroom()->Distribute()->stop($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 恢复聊天室消息分发
     * @param string $chatroomId 聊天室 id
     * @return bool|Response
     */
    public function resumeDistributeChatroom(string $chatroomId): bool|Response
    {
        $chatroom = [
            'id' => $chatroomId
        ];
        $result = $this->rong_cloud->getChatroom()->Distribute()->resume($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 添加聊天室成员禁言
     * @param string $chatroomId 聊天室 id
     * @param string $userId 禁言人员 id
     * @param int $minute 禁言时长
     * @return bool|Response
     */
    public function addGagChatroom(string $chatroomId, string $userId, int $minute = 30): bool|Response
    {
        $chatroom = [
            'id' => $chatroomId,
            'members' => [
                ['id' => $userId]
            ],
            'minute' => $minute
        ];
        $result = $this->rong_cloud->getChatroom()->Gag()->add($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 解除聊天室成员禁言
     * @param string $chatroomId 聊天室 id
     * @param string $userId 禁言人员 id
     * @return bool|Response
     */
    public function removeGagChatroom(string $chatroomId, string $userId): bool|Response
    {
        $chatroom = [
            'id' => $chatroomId,
            'members' => [
                ['id' => $userId]
            ],
        ];
        $result = $this->rong_cloud->getChatroom()->Gag()->remove($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 解除聊天室成员禁言
     * @param string $chatroomId 聊天室 id
     * @return bool|Response
     */
    public function getListGagChatroom(string $chatroomId): bool|Response
    {
        $chatroom = [
            'id' => $chatroomId,
        ];
        $result = $this->rong_cloud->getChatroom()->Gag()->getList($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return $result;
    }


    /**
     * 添加保活聊天室
     * @param string $chatroomId 聊天室 id
     * @return bool|Response
     */
    public function addKeepaliveChatroom(string $chatroomId): bool|Response
    {
        $chatroom = [
            'id' => $chatroomId,
        ];
        $result = $this->rong_cloud->getChatroom()->Keepalive()->add($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 删除保活聊天室
     * @param string $chatroomId 聊天室 id
     * @return bool|Response
     */
    public function removeKeepaliveChatroom(string $chatroomId): bool|Response
    {
        $chatroom = [
            'id' => $chatroomId,
        ];
        $result = $this->rong_cloud->getChatroom()->Keepalive()->remove($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return true;
    }

    /**
     * 获取保活聊天室
     * @return bool|Response
     */
    public function getListKeepaliveChatroom(): bool|Response
    {
        $chatroom = [
        ];
        $result = $this->rong_cloud->getChatroom()->Keepalive()->getList($chatroom);
        $code = $result['code'];
        if ($code != 200) {
            return errorMessage($result['msg']);
        }
        return $result;
    }


    /**
     * 添加聊天室用户白名单
     * @param string $chatroomId 聊天室ID
     * @param array $members [["id"=>"user1"], //用户 id  ["id"=>"user2"]]
     * @return Response|bool|array
     */
    public function addWhitelistChatroom(string $chatroomId, array $members): Response|bool|array
    {
        $chatroom = [
            "id" => $chatroomId,
            "members" => $members
        ];
        $data_result = $this->rong_cloud->getChatroom()->Whitelist()->User()->add($chatroom);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 移除聊天室用户白名单
     * @param string $chatroomId 聊天室ID
     * @param array $members [["id"=>"user1"], //用户 id  ["id"=>"user2"]]
     * @return Response|bool|array
     */
    public function removeUserBlackWhitelistChatroom(string $chatroomId, array $members): Response|bool|array
    {
        $chatroom = [
            "id" => $chatroomId,
            "members" => $members
        ];
        $data_result = $this->rong_cloud->getChatroom()->Whitelist()->User()->remove($chatroom);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }


    /**
     * 获取聊天室用户白名单
     * @param string $chatroomId 聊天室ID
     * @return Response|bool|array
     */
    public function getUserWhitelistChatroom(string $chatroomId): Response|bool|array
    {
        $chatroom = [
            "id" => $chatroomId,
        ];
        $data_result = $this->rong_cloud->getChatroom()->Whitelist()->User()->getList($chatroom);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_result;
    }


    /**
     * 设置聊天室属性（KV）
     * @param string $chatroomId 聊天室 id
     * @param string $userId 操作用户 Id
     * @param string $key 聊天室属性名称
     * @param string $value 聊天室属性对应的值
     * @return Response|bool|array
     */
    public function setEntryChatroom(string $chatroomId, string $userId, string $key, string $value): Response|bool|array
    {
        $params = [
            'id' => $chatroomId,
            'userId' => $userId,
            'key' => $key,
            'value' => $value,
        ];
        $data_result = $this->rong_cloud->getChatroom()->Entry()->set($params);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 批量设置聊天室属性（KV）
     * @param string $chatroomId 聊天室 id
     * @param string $userId 聊天室自定义属性的所属用户 ID
     * @param array $entryInfo 聊天室属性对应的值
     * @param int $autoDelete 用户（entryOwnerId）退出聊天室后，是否删除此 Key 值
     * @return Response|bool|array
     */
    public function batchSetEntryChatroom(string $chatroomId, string $userId, array $entryInfo, int $autoDelete = 0): Response|bool|array
    {
        $params = [
            'id' => $chatroomId,
            'autoDelete' => 0,
            'entryOwnerId' => $userId,
            'entryInfo' => json_encode($entryInfo),
        ];
        $data_result = $this->rong_cloud->getChatroom()->Entry()->batchSet($params);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 获取聊天室属性
     * @param string $chatroomId 聊天室 id
     * @return Response|bool|array
     */
    public function queryEntryChatroom(string $chatroomId): Response|bool|array
    {
        $params = [
            'id' => $chatroomId,
        ];
        $data_result = $this->rong_cloud->getChatroom()->Entry()->query($params);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_result;
    }

    /**
     * 删除聊天室属性
     * @param string $chatroomId 聊天室 id
     * @param string $userId 操作用户 Id
     * @param string $key 聊天室属性名称
     * @return Response|bool|array
     */
    public function removeEntryChatroom(string $chatroomId, string $userId, string $key): Response|bool|array
    {
        $params = [
            'id' => $chatroomId,
            'userId' => $userId,
            'key' => $key,
        ];
        $data_result = $this->rong_cloud->getChatroom()->Entry()->remove($params);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_result;
    }


    /**
     * 添加聊天室全体禁言
     * @param string $chatroomId 聊天室 id
     * @param string $userId 聊天室自定义属性的所属用户 ID
     * @param array $entryInfo 聊天室属性对应的值
     * @param int $autoDelete 用户（entryOwnerId）退出聊天室后，是否删除此 Key 值
     * @return Response|bool|array
     */
    public function addMuteAllMembersChatroom(string $chatroomId, string $userId, array $entryInfo, int $autoDelete = 0): Response|bool|array
    {
        $params = [
            'id' => $chatroomId,
        ];
        $data_result = $this->rong_cloud->getChatroom()->MuteAllMembers()->add($params);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 解除聊天室全体禁言
     * @param string $chatroomId 聊天室 id
     * @param string $userId 聊天室自定义属性的所属用户 ID
     * @param array $entryInfo 聊天室属性对应的值
     * @param int $autoDelete 用户（entryOwnerId）退出聊天室后，是否删除此 Key 值
     * @return Response|bool|array
     */
    public function removeMuteAllMembersChatroom(string $chatroomId, string $userId, array $entryInfo, int $autoDelete = 0): Response|bool|array
    {
        $params = [
            'id' => $chatroomId,
        ];
        $data_result = $this->rong_cloud->getChatroom()->MuteAllMembers()->remove($params);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 聊天室全体禁言状态检查
     * @param string $chatroomId 聊天室 id
     * @param string $userId 聊天室自定义属性的所属用户 ID
     * @param array $entryInfo 聊天室属性对应的值
     * @param int $autoDelete 用户（entryOwnerId）退出聊天室后，是否删除此 Key 值
     * @return Response|bool|array
     */
    public function checkMuteAllMembersChatroom(string $chatroomId, string $userId, array $entryInfo, int $autoDelete = 0): Response|bool|array
    {
        $params = [
            'id' => $chatroomId,
        ];
        $data_result = $this->rong_cloud->getChatroom()->MuteAllMembers()->check($params);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 获取聊天室全体禁言列表
     * @param int $page
     * @param int $size
     * @return mixed|Response|null
     */
    public function getListMuteAllMembersChatroom(int $page = 1, int $size = 10): mixed
    {
        $data_result = $this->rong_cloud->getChatroom()->MuteAllMembers()->getList($page, $size);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_result;
    }

    /**
     * 添加聊天室全体禁言白名单
     * @param string $chatroomId
     * @param array $members
     * @return Response|bool|array
     */
    public function addMuteWhiteListChatroom(string $chatroomId, array $members): Response|bool|array
    {
        $chatroom = [
            "id" => $chatroomId,
            "members" => $members
        ];
        $data_result = $this->rong_cloud->getChatroom()->MuteWhiteList()->add($chatroom);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 移除聊天室全体禁言白名单
     * @param string $chatroomId
     * @param array $members
     * @return Response|bool|array
     */
    public function removeMuteWhiteListChatroom(string $chatroomId, array $members): Response|bool|array
    {
        $chatroom = [
            "id" => $chatroomId,
            "members" => $members
        ];
        $data_result = $this->rong_cloud->getChatroom()->MuteWhiteList()->remove($chatroom);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 获取聊天室全体禁言列表白名单
     * @param string $chatroomId
     * @return mixed|Response|null
     */
    public function getListMuteWhiteListChatroom(string $chatroomId): mixed
    {
        $chatroom = [
            "id" => $chatroomId,
        ];
        $data_result = $this->rong_cloud->getChatroom()->MuteWhiteList()->getList($chatroom);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_result;
    }


    /**
     * 添加敏感词
     * @param string $keyword 敏感词
     * @param int $type 0: 敏感词替换 1: 敏感词屏蔽
     * @param string $replace 敏感词替换，最长不超过 32 个字符， 敏感词屏蔽可以为空
     * @return Response|bool|array
     */
    public function addSensitive(string $keyword, int $type = 0, string $replace = "***"): Response|bool|array
    {
        $sensitive = [
            'replace' => $replace,
            'keyword' => $keyword,
            'type' => $type
        ];
        $data_result = $this->rong_cloud->getSensitive()->add($sensitive);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 删除敏感词
     * @param string $keyword 敏感词
     * @return Response|bool|array
     */
    public function removeSensitive(array $keyword): Response|bool|array
    {
        $sensitive = [
            'keywords' => $keyword,
        ];
        $data_result = $this->rong_cloud->getSensitive()->remove($sensitive);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }

    /**
     * 删除敏感词
     * @param string $type 敏感词类型，0: 敏感词替换， 1: 敏感词屏蔽， 为空获取全部
     * @return Response|bool|array
     */
    public function getListSensitive(string $type =''): Response|bool|array
    {
        $sensitive = [
            'type'=> $type,
        ];
        $data_result = $this->rong_cloud->getSensitive()->getList($sensitive);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return $data_result;
    }


    /**
     * @param array $notification 按平台（操作系统）指定推送内容
     * @param array $audience 推送条件。支持按用户标签推送（tag 、tag_or）、按应用包名推送（packageName）和按指定平台全部推送（is_to_all）。注意：如果推送条件中 is_to_all 为 true，则忽略其他推送条件。
     * @param array $platform 目标平台（操作系统），可以为 ios、android 其中一个或全部。全部填写时给给 Android、iOS 两个平台推送消息。
     * @return Response|bool|array
     */
    public function addPush(array $notification,array $audience,array $platform=['ios','android']): Response|bool|array
    {
        $sensitive = [
            'platform'=> $platform,//目标操作系统
            'audience'=>['is_to_all'=>true],//推送条件，包括： tag、userid、is_to_all。
            'notification'=>['alert'=>$notification]
        ];
        $data_result = $this->rong_cloud->getPush()->push($sensitive);
        $code = $data_result['code'];
        if ($code != 200) {
            return errorMessage($data_result['msg']);
        }
        return true;
    }
}
